insuranceApp
    .controller('homeCtrl', ['$scope', 'myFunction', 'insuranceHttp', function ($scope, myFunction, insuranceHttp){
        $scope.urlData = myFunction.getUrlParam(location.href)
        $scope.userInfo = {
            channel: $scope.urlData.channelCode,
            business: $scope.urlData.businessCode
        }
        $scope.getAge = function (v) {
            var myDate = new Date()
            var month = myDate.getMonth() + 1
            var day = myDate.getDate()
            var age = myDate.getFullYear() - v.substring(6, 10) - 1
            if (v.substring(10, 12) < month || v.substring(10, 12) == month && v.substring(12, 14) <= day) {
                age++
            }
            return age
        }
        $scope.next = function () {
            $scope.age = $scope.getAge($scope.userInfo.cardNo)
            if(!myFunction.verifyId($scope.userInfo.cardNo)){
                $scope.error = '请填写正确身份证号'
                return
            }
            if($scope.age<18 || $scope.age > 45){
                $scope.error = '本次活动仅限18-45周岁'
                return
            }
            $scope.error = '跳转中，请等待...'
            if(!$scope.btnDisabled){
                $scope.btnDisabled = true
                insuranceHttp.create({}, $scope.userInfo, function (data) {
                    $scope.btnDisabled = false
                    if(data.code == 0){
                        $scope.error = false
                        insuranceHttp.goPingan({}, {
                            channelCode: $scope.urlData.channelCode,
                            businessCode: $scope.urlData.businessCode,
                            orderCode: data.data.id,
                            realName: $scope.userInfo.name,
                            idNo: $scope.userInfo.cardNo
                        }, function (data) {
                            if(data.success){
                                location.href = data.obj.openUrl
                            }else {
                                $scope.error = data.msg
                            }
                        })
                    }else{
                        $scope.error = data.message
                    }
                })
            }
        }
    }])